package com.petcommunity.servlet;

import com.petcommunity.dao.*;
import com.petcommunity.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/edit")
public class EditServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String type = request.getParameter("type");
        int id = Integer.parseInt(request.getParameter("id"));
        Object entity = null;

        switch (type) {
            case "pet":
                entity = new PetDAO().getPetById(id);
                break;
            case "owner":
                entity = new OwnerDAO().getOwnerById(id);
                break;
            case "adoption":
                entity = new AdoptionDAO().getAdoptionById(id);
                break;
            case "lost":
                entity = new LostPetDAO().getLostPetById(id);
                break;
            case "vaccine":
                entity = new VaccineRecordDAO().getVaccineById(id);
                break;
            case "notice":
                entity = new NoticeDAO().getNoticeById(id);
                break;
            default:
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "无效类型");
                return;
        }

        request.setAttribute("type", type);
        request.setAttribute("entity", entity);
        request.getRequestDispatcher("/edit/edit" + type.substring(0, 1).toUpperCase() + type.substring(1) + ".jsp").forward(request, response);
    }
}

